package com.diudiutechnology.www.yadiudiu.util.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.AbstractWheelTextAdapter1;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.OnWheelChangedListener;
import com.diudiutechnology.www.yadiudiu.util.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/5.
 */

public class MerchantsTypePhotoPop extends PopupWindow implements View.OnClickListener {
    Context context;
    private WheelView wvProvince;
    private View lyChangeAddress;
    private View lyChangeAddressChild;
    private TextView btnSure;
    private TextView btnCancel;
    private OnAddressCListener onAddressCListener;
    private String strProvince="";
    List<MerchantsTypeModel> typeModels;
    private AddressTextAdapter provinceAdapter;
    private int maxsize = 14;
    private int minsize = 12;
    public MerchantsTypePhotoPop(final Context context, List<MerchantsTypeModel> typeModels) {
        super(context);
        this.context = context;
        this.typeModels=typeModels;
        View view = View.inflate(context, R.layout.photo_pop_merchants_type, null);

        wvProvince = (WheelView) view.findViewById(R.id.wv_address_province);
        lyChangeAddress = view.findViewById(R.id.ly_myinfo_changeaddress);
        lyChangeAddressChild = view.findViewById(R.id.ly_myinfo_changeaddress_child);
        btnSure = (TextView) view.findViewById(R.id.btn_myinfo_sure);
        btnCancel = (TextView) view.findViewById(R.id.btn_myinfo_cancel);
        strProvince=typeModels.get(0).getTypename();

        //设置SelectPicPopupWindow的View
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//		this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        lyChangeAddressChild.setOnClickListener(this);
        btnSure.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        provinceAdapter = new AddressTextAdapter(context, typeModels,0,maxsize, minsize);
        wvProvince.setVisibleItems(5);
        wvProvince.setViewAdapter(provinceAdapter);
        wvProvince.setCurrentItem(0);
        wvProvince.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
                System.out.println("stringmd5result"+wheel.getCurrentItem()+currentText);
                strProvince = currentText;
                setTextviewSize(currentText, provinceAdapter);
            }
        });
    }

    private class AddressTextAdapter extends AbstractWheelTextAdapter1 {
       List<MerchantsTypeModel> list;

        protected AddressTextAdapter(Context context, List<MerchantsTypeModel> list, int currentItem,int maxsize, int minsize) {
            super(context, R.layout.item_birth_year, NO_RESOURCE,currentItem ,maxsize, minsize);
            this.list = list;
            setItemTextResource(R.id.tempValue);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index).getTypename() + "";
        }

    }

    public void setAddresskListener(OnAddressCListener onAddressCListener) {
        this.onAddressCListener = onAddressCListener;
    }

    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, AddressTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(14);
            } else {
                textvew.setTextSize(12);
            }
        }
    }
    @Override
    public void onClick(View view) {
        if (view == btnSure) {
            if (onAddressCListener != null) {
                onAddressCListener.onClick(strProvince);
            }
        } else if (view == btnCancel) {

        } else if (view == lyChangeAddressChild) {
            return;
        } else {
//			dismiss();
        }
        dismiss();
    }
    /**
     * 回调接口
     *
     * @author Administrator
     *
     */
    public interface OnAddressCListener {
        public void onClick(String province);
    }
}